'use strict';

const Controller = require('egg').Controller;
const moment = require('moment');
/**
 * 200=成功
 * 3001-3999=错误码 
 */
class HrbeuController extends Controller {

    /**
     * 签到
     */
    async sign() {
        const { ctx, config, app } = this;
        let { user_id = "zhoukaidong", way = "" } = ctx.request.body;
        let nowTime = moment(Date.now()).format("YYYY-MM-DD");

        let rowData = await ctx.model.ProSexual.findOne({ "where": { user_id, create_time: nowTime + " 08:00:00" } });
        if (rowData) {
            ctx.body = {
                code: 3001,
                msg: "失败",
                data: {}
            };
        } else {
            let uid = await app.utils.uuid.v1();
            await ctx.model.ProSexual.create({ uid, user_id, way, status: "01", create_time: nowTime });
            ctx.body = {
                code: 200,
                msg: "成功",
                data: {}
            };
        }

    }

    /**
     * 保存
     */
    async getDataList() {
        const { ctx, config, app } = this;
        let { user_id = "zhoukaidong" } = ctx.request.body;
        let datalist = await ctx.model.query("SELECT t.uid,t.way,t.status,DATE_FORMAT(t.create_time,'%Y-%m-%d') create_time FROM pro_sexual t where t.user_id=:user_id and t.create_time>(SELECT t2.create_time FROM pro_sexual t2 where t.user_id=:user_id and t2.status='00' LIMIT 1) order by t.create_time desc",
            { replacements: { user_id }, raw: true, type: app.Sequelize.QueryTypes.SELECT });
        ctx.body = {
            code: 200,
            msg: "成功",
            data: {
                datalist
            }
        };
    }

    /**
     * 获取临时的id
     */
    async getHrbTempId() {
        const { ctx, app } = this;
        let { temp_id } = ctx.request.body;
        //清空
        if (!temp_id) {
            temp_id = await app.utils.uuid.v1();
            await ctx.model.HrbeuWinter.create({ id: temp_id, temp_id, user_id: temp_id, create_time: new Date() });
        }
        ctx.body = {
            code: 200,
            msg: "成功",
            data: {
                temp_id
            }
        };
    }


}

module.exports = HrbeuController;